<?php
/*
 * @@Copyright (C), 2021-2022: 恒盛时代
 * @Author: zhouzhou
 * @Date: 2021/12/22 11:59
 * @Description: 用户转发记录表
 */

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;

class ImUserForward extends BaseModel
{
    protected $table = 'im_user_forward';
    public $fillable = ['user_id'];
    protected $hidden = ['created_at','updated_at'];

    /**
     * 关联用户表
     */
    public function user()
    {
        return $this->belongsTo(ImUsers::class, 'other_uid', 'id')
            ->select(['id', 'nickname', 'headimg']);
    }

    /**
     * 关联群组表
     */
    public function group()
    {
        return $this->belongsTo(ImGroup::class, 'im_gid', 'im_gid')
                ->select('im_gid','name','description','face');;
    }

    /**
     * @desc 用户新增转发
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/12/22 14:48
     */
    public function addForward($input, $userId = 0)
    {
        $otherUids = $input['other_uids'] ?? [];
        $typeData = $input['type'] ?? [];

        foreach ($otherUids as $key =>&$val)
        {
            $type = $typeData[$key];
            $this->forwardHandle($userId, $val, $type, $key,$typeData);
        }

        return ['code' => 0, 'msg' => '转发成功'];
    }

    /**
     * @desc 封装插入的转发表
     * @param int $userId 用户
     * @param int $val 转发的值
     * @param int $type 1用户 2群组
     * @param int $key key 的键值
     * @param array $typeData 类型的数组
     * @author zhouzhou
     * @time 2021/12/22 14:46
     */
    public function forwardHandle($userId, $val, $type, $key, $typeData)
    {
        $forward = [];
        if ($type == 1)
        {
            $forwardInfo = $this->where(['user_id' => $userId, 'other_uid' => $val])->first();
        }
        else
        {
            $forwardInfo = $this->where(['user_id' => $userId, 'im_gid' => $val])->first();
        }

        if ($forwardInfo)
        {
            $forwardInfo->updated_at = time();
            $forwardInfo->save();
        }
        else
        {
            $forward[$key]['user_id'] = $userId;
            $forward[$key]['other_uid'] = $typeData[$key] == 1 ? $val : 0;
            $forward[$key]['im_gid'] = $typeData[$key] == 2 ? $val : 0;
            $forward[$key]['created_at'] = time();
            $forward[$key]['updated_at'] = time();
            $this->insert($forward);
        }

        return true;
    }

    /**
     * @desc 转发列表
     * @param int $userId 用户id
     * @param int $limit 每页默认10个
     * @param int $offset 从那条数据开始
     * @return mixed
     * @author zhouzhou
     * @time 2021/11/23 16:50
     */
    public function forwardList($userId = 0, $limit = 10, $offset = 0)
    {
        $recentForwqrd = $this->select('id', 'user_id', 'other_uid', 'im_gid')->with(['user','group'])->where(['user_id'=>$userId])
            ->orderBy('updated_at', 'desc')
            ->limit(5)
            ->offset($offset)
            ->get();

        $list = $this->select('id', 'user_id', 'other_uid', 'im_gid')->with(['user','group'])->where(['user_id'=>$userId])
            ->orderBy('updated_at', 'desc')
            ->limit($limit)
            ->offset($offset)
            ->get();

        return ['recent'=>$recentForwqrd, 'list'=>$list];
    }


}
